package com.aptemo.webetek.client.widgets.teams;

import java.util.List;
import java.util.MissingResourceException;

import com.aptemo.webetek.client.Webetek;
import com.aptemo.webetek.client.WebetekUserServiceAsync;
import com.aptemo.webetek.client.widgets.paging.PagingDisplay;
import com.aptemo.webetek.client.widgets.paging.PagingView;
import com.aptemo.webetek.shared.TeamBean;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Document;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.HasKeyPressHandlers;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class UserGroups
		extends Composite implements UserGroupsPresenter.Display
{

	private static UserGroupUiBinder	uiBinder	= GWT.create(UserGroupUiBinder.class);

	interface UserGroupUiBinder extends UiBinder<Widget, UserGroups>
	{
	}

	//

	@UiField
	Button		createButton;

	@UiField
	FlexTable	contentPanel;

	@UiField
	CheckBox	showMineOnly;

	@UiField
	TextBox		searchField;

	@UiField
	HTML		infoField;

	@UiField
	PagingView	pagingControls;

	// @UiField
	// Hyperlink moreLink;

	Timer		timer;

	public UserGroups()
	{
		initWidget(uiBinder.createAndBindUi(this));

		timer = new Timer()
		{
			@Override
			public void run()
			{
				ChangeEvent.fireNativeEvent(Document.get().createChangeEvent(), searchField);
				// searchField.setFocus(false);
			}
		};

	}

	@Override
	public HasClickHandlers createTeamBtn()
	{
		return createButton;
	}

	@Override
	public void updateTeams(List<TeamBean> list, HandlerManager eventbus, WebetekUserServiceAsync rpc)
	{
		contentPanel.removeAllRows();

		pagingControls.setVisible(true);

		int i = 0;
		for (TeamBean item : list)
		{
			UserGroupRow row = new UserGroupRow();
			UserGroupRowPresenter presenter = new UserGroupRowPresenter(eventbus, rpc, row, item);
			presenter.go(null);
			contentPanel.setWidget(i++, 0, row);
		}

	}

	@Override
	public void addAdminMenu()
	{
		createButton.setVisible(true);
		showMineOnly.setVisible(true);
	}

	@Override
	public Widget getDisplay()
	{
		return this;
	}

	@Override
	public HasValueChangeHandlers<Boolean> getShowMineOnlyGroups()
	{
		return showMineOnly;
	}

	@Override
	public HasValue<Boolean> getShowMineOnlyGroupsValue()
	{
		return showMineOnly;
	}

	@Override
	public HasClickHandlers getList()
	{
		return contentPanel;
	}

	@Override
	public int getClickedRow(ClickEvent event)
	{

		int selectedRow = -1;
		HTMLTable.Cell cell = contentPanel.getCellForEvent(event);

		if (cell != null)
		{
			// Suppress clicks if the user is actually selecting the
			// check box
			if (cell.getCellIndex() >= 0)
			{
				selectedRow = cell.getRowIndex();
			}
		}

		return selectedRow;
	}

	@Override
	public HasValueChangeHandlers<String> getFilterTextBox()
	{
		return searchField;
	}

	@Override
	public HasValue<String> getFilterTextBoxValue()
	{
		return searchField;
	}

	@Override
	public HasKeyPressHandlers getFilterTextBoxKeyPressed()
	{
		return searchField;
	}

	@UiHandler("searchField")
	void keypressedHandler(KeyPressEvent event)
	{
		timer.cancel();
		timer.schedule(1000);
	}

	@Override
	public HasChangeHandlers getFilterTextBoxChange()
	{
		return searchField;
	}

	@Override
	public HasText getInfoField()
	{
		return infoField;
	}

	@Override
	public PagingDisplay getPagingView()
	{
		return pagingControls;
	}

	@Override
	public void setPagingMode(boolean showPaging)
	{
		// moreLink.setVisible(!showPaging);
		pagingControls.setVisible(showPaging);
	}

	@Override
	public String translate(String key)
	{
		try
		{
			return Webetek.getConstants().getString(key);
		}
		catch (MissingResourceException e)
		{
			return key;
		}
	}

}
